import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
// import Mock from './mock'
// Mock.bootstrap();

import 'font-awesome/css/font-awesome.min.css'
import axios from 'axios'
//配置axios的全局基本路径
axios.defaults.baseURL='http://localhost:8080/'
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios
Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)
// 百度地图
import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
  ak: "LSNHTIDvIKtkv7UgAUKfStSz4QU74osS"
})
//NProgress.configure({ showSpinner: false });

const router = new VueRouter({
  routes
})
// 前端拦截器
// 将token携带到后端
router.beforeEach((to, from, next) => {
  // alert(1);
  //NProgress.start();
  // 判断请求
  if (to.path == '/login' || to.path == "/shopRegister") {
    // 移除绑定得数据
    localStorage.removeItem('token');
    localStorage.removeItem('loginInfo');
    next();//放行
  }else{
    // 获取loginInfo
    let loginInfo = localStorage.getItem('loginInfo');
    // alert(loginInfo);
    if (!loginInfo) {
      next({path: '/login'});//跳转到login
    } else {
      // alert(3)
      next()
    }
  }
})

//后置拦截器【处理后台登录拦截的结果】====================//
axios.interceptors.response.use(res => {
  // alert(2);
  if ("noLogin" === res.data.msg) {
    // alert(2);
    localStorage.removeItem("token");
    localStorage.removeItem("loginInfo");
    router.push({path: '/login'});
  }
  return res;
},error => {
  Promise.reject(error)
})

//前置拦截器【拦截调用后端的请求
axios.interceptors.request.use(res=>{
  // alert(3);
  // 获取token

  let token = localStorage.getItem("token");
  // alert(localStorage.getItem("token"))
  // alert(token)
  // 有token则在请求头里携带token
  if(token){
    res.headers["token"] = token;
    // alert(1)
  }
  return res;
},error => {
  Promise.reject(error)
})



//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

